﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    DAL DataAccessLayer;
    DataSet personDataSet;
    TextBox[] ArrayTextBox;
    TextBox[] SearchTextBox;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.DataAccessLayer = new DAL();
        this.personDataSet = DataAccessLayer.GetAllPersons();
        this.phoneGridView.DataSource = personDataSet;
        this.phoneGridView.DataBind();

        SearchTextBox = new TextBox[] {SName, SSurName, Stel};

        for (int i = 0; i < SearchTextBox.Length; ++i)
        {
            SearchTextBox[i].AutoPostBack = true;
            SearchTextBox[i].TextChanged += _Default_TextChanged;
        }


    }

    void _Default_TextChanged(object sender, EventArgs e)
    {
        string filter = null;
        DataView DW = null;
        string[] CN = { "FirstName", "LastName", "Photo_x0020_1" };

        for (int i = 0; i < SearchTextBox.Length-1; ++i)
        {
            if (i > 0) filter += " and ";
            filter += CN[i]+" like " + "'%" + SearchTextBox[i].Text.Trim() + "%'";
        }
        DW = personDataSet.Tables[0].AsDataView();
        DW.RowFilter = filter;

        phoneGridView.DataSource = DW;
        phoneGridView.DataBind();
    }

    protected void phoneGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        ArrayTextBox = new[] {Name, SurName, County, City, Street, House, Tel1, Tel2};
        for (int i = 0; i < ArrayTextBox.Length; ++i)
        {
            this.ArrayTextBox[i].Text = phoneGridView.SelectedRow.Cells[i+3].Text;
        }
        
    }
    protected void ShowAllRecords_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < SearchTextBox.Length - 1; ++i)
        {
           SearchTextBox[i].Text = "";
        }
        this.DataAccessLayer = new DAL();
        this.personDataSet = DataAccessLayer.GetAllPersons();
        this.phoneGridView.DataSource = personDataSet;
        this.phoneGridView.DataBind();        
    }
}